package com.xiaogao.common.config.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义线程工厂（给线程命名，便于问题排查）
 */
public class NamedThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger threadNumber = new AtomicInteger(1);

    NamedThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix + "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r, namePrefix + threadNumber.getAndIncrement());
        // 设置为非守护线程（防止主线程退出时被回收）
        t.setDaemon(false);
        // 设置优先级（普通优先级）
        t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}
